package com.oss.util;

import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author chengqiang
 * 下载工具类
 */
@Data
public class FileUtilCustom {
    /**
     * 下载文件
     * @param inputStream   excel文件输入流
     * @param fileName      文件名称
     */
    public static void downloadFile(InputStream inputStream, String parentPath, String fileName) {
        OutputStream outputStream = null;
        try {
            File parentDir = new File(parentPath);
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            outputStream = Files.newOutputStream(Paths.get(parentPath + fileName));
            byte[] buffer = new byte[4096];
            int length;
            while ((length=inputStream.read(buffer))!=-1) {
                outputStream.write(buffer,0,length);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    /**
     * 下载文件
     * @param inputStream   excel文件输入流
     * @param fileName      文件名称
     * @param response      响应对象
     */
    public static void downloadFile(InputStream inputStream, String fileName, HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            byte[] buffer = new byte[4096];
            int length;
            while ((length=inputStream.read(buffer))!=-1) {
                outputStream.write(buffer,0,length);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    /**
     * 下载文件
     * @param fileText      待写出的文件内容
     * @param fileName      文件名称
     * @param response      响应对象
     */
    public static void downloadFile(String fileText, String fileName, HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            byte[] buffer = new byte[4096];
            outputStream.write(fileText.getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    public static void downloadFileByPath( String filePath, String fileName, HttpServletResponse response) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            File parentDir = new File(filePath);
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            inputStream = new FileInputStream(filePath);
            outputStream = response.getOutputStream();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            byte[] buffer = new byte[4096];
            int length;
            while ((length=inputStream.read(buffer))!=-1) {
                outputStream.write(buffer,0,length);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    /**
     * 复制文件
     * @param sourcePath        源文件
     * @param destinationPath   目标文件地址
     * @throws IOException
     */
    public static void copyFile(String sourcePath, String destinationPath){
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(sourcePath);
            outputStream = new FileOutputStream(destinationPath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
